$(function(){
	initTable();
	initBtn();
});

function initTable(){
	$('#table').bootstrapTable({
		url:basePath+'member/list.htm',
		method:'post',
		columns:[
			{field : '',checkbox:true ,align: 'center',valign: 'middle'}, 
			{field : "name",title : "账户",sortable:true,width:"80px"},
			{field : "nickName",title : "昵称",sortable:true,width:"80px"},
			{field : "mobile",title : "手机号",sortable:true,width:"50px"},
			{field : "email",title : "邮箱",sortable:true,width:"100px"},
			{field : "province",title : "所在省份",sortable:true,width:"80px"},
			{field : "city",title : "所在城市",sortable:true,width:"50px"},
			{field : "address",title : "详细地址",sortable:true},
			{field : "qq",title : "QQ",sortable:true,width:"50px"},
			{field : "password",title : "登录密码",sortable:true,width:"60px"},
			{field : "twoPassword",title : "支付密码",sortable:true,width:"60px"},
			{field : "regtime",title : "注册时间",sortable:true},
			{field : "loginTime",title : "最后登陆时间",sortable:true},
			{field : "invokeCode",title : "邀请码",sortable:true,width:"200px"},
			{field : "status",title : "状态",sortable:true,formatter:statusFomatter,width:"200px"},
			{field : "id",title : "操作",sortable:true,formatter:idFormatter,width:"200px"}
		],
		clickToSelect:true,
		onLoadSuccess : function(data){},
		toolbar : '.bottom-oprea'
	});
}

function idFormatter(id){
	var btns = [];
	var updateBtn = "<a class='btn btn-info' onclick=\"addYuE('"+id + "');return false;\">添加余额</a>";
	btns.push(updateBtn);
	return btns.join(" ");
}

function addYuE(id){
	var number = window.prompt("请输入您要充值的金额：");
	if(isNaN(number) || number == ''){
		alert("请输入数字！");
	}else{
		$.post(basePath+"finance/addYuE.htm",{"id":id,"number":number},function(data){
			if(data.status == "success"){
				console.log("success");	
			}else{
				console.log("error");
			}
		});
	}
}

function statusFomatter(value,row,index){
	if(value == 0){
		return "正常";
	}else{
		return "禁用";
	}
}

function initBtn(){
	$("#searchBtn").on("click", searchFN);
	$("#batchDisabledBtn").on("click",batchDisabledStatusFn);
	$("#batchAbledBtn").on("click",batchAbledStatusFn);
}

/**
 * 搜索
 * @returns
 */
function searchFN(){
	var queryParam = {};
	var params = $("#searchForm").serializeArray();
	$.each(params,function(i,param){
		queryParam[param.name] = param.value;
	});
	$('#table').bootstrapTable('refresh', {
		query : queryParam
	});
}

function batchDisabledStatusFn(){
	// 获取所有选中的行
	var selIds = [];
	var selectRows = $('#table').bootstrapTable('getAllSelections');
	$.each(selectRows,function(i,row){
		selIds.push(row.id);
	});
	if(selIds.length == 0){
		toastr.warning("请选择要禁用的会员!");
		return false;
	}
	var disabledStatusConfirm = $.confirm({
		title: '禁用确认!',
		content: '确定要禁用所选会员吗?',
		confirm: function(){
			$.post(basePath+"member/disabledStatus.htm",{"selIds":selIds.join(",")},
					function(data){
				if(data.status == "success"){
					searchFN();
					disabledStatusConfirm.close();
				}else{
					toastr.error(data.message);
				}
			}
			,"json");
		}
	});
}

function batchAbledStatusFn(){
	// 获取所有选中的行
	var selIds = [];
	var selectRows = $('#table').bootstrapTable('getAllSelections');
	$.each(selectRows,function(i,row){
		selIds.push(row.id);
	});
	if(selIds.length == 0){
		toastr.warning("请选择要启用的会员!");
		return false;
	}
	var disabledStatusConfirm = $.confirm({
		title: '启用确认!',
		content: '确定要启用所选会员吗?',
		confirm: function(){
			$.post(basePath+"member/abledStatus.htm",{"selIds":selIds.join(",")},
					function(data){
				if(data.status == "success"){
					searchFN();
					disabledStatusConfirm.close();
				}else{
					toastr.error(data.message);
				}
			}
			,"json");
		}
	});
}